home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / deviceuricombobox.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  4.6 KB  |  155 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Std Lib
  23.  
  24. # Local
  25. from base.g import *
  26. from ui_utils import *
  27. from base import device
  28.  
  29. # Qt
  30. from PyQt4.QtCore import *
  31. from PyQt4.QtGui import *
  32.  
  33.  
  34. DEVICEURICOMBOBOX_TYPE_PRINTER_ONLY = 0
  35. DEVICEURICOMBOBOX_TYPE_FAX_ONLY = 1
  36. DEVICEURICOMBOBOX_TYPE_PRINTER_AND_FAX = 2
  37.  
  38.  
  39. class DeviceUriComboBox(QWidget):
  40.     def __init__(self, parent):
  41.         QWidget.__init__(self, parent)
  42.         self.device_uri = ''
  43.         self.initial_device = None
  44.         self.updating = False
  45.         self.typ = DEVICEURICOMBOBOX_TYPE_PRINTER_ONLY
  46.         self.filter = None
  47.         self.devices = None
  48.         self.initUi()
  49.  
  50.  
  51.     def initUi(self):
  52.         HBoxLayout = QHBoxLayout(self)
  53.         HBoxLayout.setObjectName("HBoxLayout")
  54.  
  55.         self.NameLabel = QLabel(self)
  56.         self.NameLabel.setObjectName("NameLabel")
  57.         HBoxLayout.addWidget(self.NameLabel)
  58.  
  59.         SpacerItem = QSpacerItem(20, 20, QSizePolicy.Minimum, QSizePolicy.Minimum)
  60.         HBoxLayout.addItem(SpacerItem)
  61.  
  62.         self.ComboBox = QComboBox(self)
  63.         sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
  64.         sizePolicy.setHorizontalStretch(0)
  65.         sizePolicy.setVerticalStretch(0)
  66.         sizePolicy.setHeightForWidth(self.ComboBox.sizePolicy().hasHeightForWidth())
  67.         self.ComboBox.setSizePolicy(sizePolicy)
  68.         self.ComboBox.setObjectName("ComboBox")
  69.         HBoxLayout.addWidget(self.ComboBox)
  70.  
  71.         self.NameLabel.setText(self.__tr("Device:"))
  72.  
  73. #        self.connect(self.ComboBox, SIGNAL("currentIndexChanged(int)"),
  74. #            self.ComboBox_currentIndexChanged)
  75.  
  76.         self.connect(self.ComboBox, SIGNAL("currentIndexChanged(const QString &)"),
  77.             self.ComboBox_currentIndexChanged)
  78.  
  79.  
  80.     def setType(self, typ):
  81.         if typ in (DEVICEURICOMBOBOX_TYPE_PRINTER_ONLY,
  82.                    DEVICEURICOMBOBOX_TYPE_FAX_ONLY,
  83.                    DEVICEURICOMBOBOX_TYPE_PRINTER_AND_FAX):
  84.             self.typ = typ
  85.  
  86.  
  87.     def setFilter(self, filter):
  88.         self.filter = filter
  89.  
  90.  
  91.     def setInitialDevice(self, device_uri):
  92.         self.initial_device = device_uri
  93.  
  94.  
  95.     def setDevices(self):
  96.         if self.typ == DEVICEURICOMBOBOX_TYPE_PRINTER_ONLY:
  97.             be_filter = ['hp']
  98.  
  99.         elif self.typ == DEVICEURICOMBOBOX_TYPE_FAX_ONLY:
  100.             be_filter = ['hpfax']
  101.             self.NameLabel.setText(self.__tr("Fax Device:"))
  102.  
  103.         else: # DEVICEURICOMBOBOX_TYPE_PRINTER_AND_FAX
  104.             be_filter = ['hp', 'hpfax']
  105.  
  106.         self.devices = device.getSupportedCUPSDevices(be_filter, self.filter)
  107.         return len(self.devices)
  108.  
  109.  
  110.     def updateUi(self):
  111.         if self.devices is None:
  112.             self.setDevices()
  113.  
  114.         self.device_index = {}
  115.  
  116.         if self.devices:
  117.             if self.initial_device is None:
  118.                 self.initial_device = user_conf.get('last_used', 'device_uri')
  119.  
  120.             self.updating = True
  121.             try:
  122.                 k = 0
  123.                 for i, d in enumerate(self.devices):
  124.                     self.ComboBox.insertItem(i, d)
  125.  
  126.                     if self.initial_device is not None and d == self.initial_device:
  127.                         self.initial_device = None
  128.                         k = i
  129.  
  130.                 self.ComboBox.setCurrentIndex(-1)
  131.             finally:
  132.                 self.updating = False
  133.  
  134.             self.ComboBox.setCurrentIndex(k)
  135.  
  136.             if len(self.devices) == 1:
  137.                 self.emit(SIGNAL("DeviceUriComboBox_oneDevice"))
  138.  
  139.         else:
  140.             self.emit(SIGNAL("DeviceUriComboBox_noDevices"))
  141.  
  142.  
  143.     def ComboBox_currentIndexChanged(self, t):
  144.         if self.updating:
  145.             return
  146.  
  147.         self.device_uri = unicode(t)
  148.         if self.device_uri:
  149.             user_conf.set('last_used', 'device_uri', self.device_uri)
  150.             self.emit(SIGNAL("DeviceUriComboBox_currentChanged"), self.device_uri)
  151.  
  152.  
  153.     def __tr(self,s,c = None):
  154.         return qApp.translate("DeviceUriComboBox",s,c)
  155.